<template>
  <el-dialog :title="title" :visible.sync="visiable" width="500px" :before-close="handleClose">
    <el-form ref="formData" :model="formData" label-width="100px" label-position="right" style="width:400px;" status-icon>
      <el-form-item label="角色名称:" prop="roleName" >
        <el-input v-model="formData.roleName" ></el-input>
      </el-form-item>
      <el-form-item label="角色描述:" prop="roleDescription">
        <el-input v-model="formData.roleDescription"></el-input>
      </el-form-item>
      <el-form-item align="center">
        <el-button type="primary" @click="submit">确定</el-button>
        <el-button @click="handleClose">取消</el-button>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import roleApi from '@/api/role';
export default {
  props:{
    title:{
      type:String,
      default:''
    },
    roleId:{
      type: String,
      default:''
    },
    visiable:{  // 弹出窗口
      type:Boolean,
      default:false
    },
    formData: { //提交表单数据
      type:Object,
      // eslint-disable-next-line vue/require-valid-default-prop
      default:{}
    },
    remoteClose: Function, //用于关闭窗口
  },

  methods: {
    // 关闭窗口
    handleClose(){
      // 将表单清空
      this.$refs['formData'].resetFields();
      // 调用父组件的方法关闭窗口
      this.remoteClose();
    },

    // 提交数据
    async submit () {
        let response = null;
        if (this.roleId) {
          // 提交编辑数据
          const data = {
            roleName: this.formData.roleName,
            roleDescription: this.formData.roleDescription,
            roleId: this.roleId
          }
          response = await roleApi.updateRole(data);
        } else {
          // 提交新增数据
          response = await roleApi.addRole(this.formData);
        }

        if (response.code === 200) {
          this.$notify({
            title: '成功',
            message: '操作成功',
            type: 'success',
            duration: 1500
          });
        } else if (response.code === 306) {
          this.$notify.error({
            title: '错误',
            message: '您没有操作权限，请联系管理员',
            duration: 1500
          });
        } else {
          this.$notify.error({
            title: '错误',
            message: '系统异常，请稍后再试',
            duration: 1500
          });
        }
        this.handleClose();
    },




  }
}

</script>
<style scoped>
</style>
